/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.testingProject.view;

import java.awt.Container;
import java.text.DateFormat;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.colswe.testingProject.control.StudentControl;
import org.colswe.testingProject.entity.Application;
import org.colswe.testingProject.entity.Student;

/**
 *
 * @author andres
 */
public class ApplicationPanel extends javax.swing.JPanel {

    private Student studentP;

    /**
     * Creates new form ApplicationPanel
     */
    public ApplicationPanel() {
        initComponents();
    }

    public ApplicationPanel(Student student) {
        initComponents();
        student = new StudentControl().read(student);
        lblNames.setText(student.getName());
        lblLastNames.setText(student.getLastNames());
        lblId.setText(student.getIdNumber());
        lblProgram.setText(student.getProgram().getName());
        lblCode.setText(student.getCode());
        tblApps.setModel(generateModel(student));

    }

    private TableModel generateModel(Student student) {
        String data[][] = null;
        String[] columns = {"Id", "Tipo", "Fecha", "Periodo", "Estado"};
        if (student.getApplications().size() > 0) {
            DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.SHORT, DateFormat.getAvailableLocales()[0]);
            data = new String[student.getApplications().size()][5];
            int i = 0;
            for (Application ap : student.getApplications()) {
                data[i][0] = "" + ap.getId();
                data[i][1] = ap.getType().getName();
                data[i][2] = dateFormatter.format(ap.getDateLastChange());
                data[i][3] = ap.getPeriodApplication();
                data[i][4] = ap.getInstance() + "-" + ap.getDecision();
                i++;
            }
            return new DefaultTableModel(data, columns);
        } else {
            return new DefaultTableModel(data, columns);
        }
    }

    public void setModel(TableModel dataModel) {
        tblApps.setModel(dataModel);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtIdNumber = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        lblNames = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lblLastNames = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblId = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lblCode = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        lblProgram = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblApps = new javax.swing.JTable();
        btnNext = new javax.swing.JButton();
        btnSearch = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(730, 650));
        setMinimumSize(new java.awt.Dimension(730, 650));

        jLabel1.setText("Cédula");

        jLabel2.setText("Nombres");

        jLabel3.setText("Apellidos");

        jLabel4.setText("Identificación");

        jLabel5.setText("Código");

        jLabel7.setText("Programa");

        jLabel8.setText("Solicitudes");

        jScrollPane1.setViewportView(tblApps);

        btnNext.setText("Siguiente");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        btnSearch.setText("Buscar");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtIdNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSearch)
                        .addGap(20, 20, 20)
                        .addComponent(btnNext)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lblId, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lblCode, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                                        .addComponent(jLabel7))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lblNames, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel3)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(lblLastNames, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                                    .addComponent(lblProgram, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addGap(52, 52, 52))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtIdNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnNext)
                    .addComponent(btnSearch))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblNames)
                    .addComponent(jLabel3)
                    .addComponent(lblLastNames))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(lblId)
                    .addComponent(jLabel5)
                    .addComponent(lblCode)
                    .addComponent(jLabel7)
                    .addComponent(lblProgram))
                .addGap(18, 18, 18)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 402, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(112, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        Container c = getParent();
        c.setVisible(false);
        c.removeAll();
        c.add(new CreateApplication(studentP));
        c.setVisible(true);
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        StudentControl control = new StudentControl();
        Student student = new Student();
        try {
            if (txtIdNumber.getText().length() > 12 || txtIdNumber.getText().length() < 6) {
                throw new Exception("La cédula debe tener entre 6 y 12 caracteres");
            }
            student.setIdNumber(txtIdNumber.getText());
            student = control.read(student);
            if (student != null) {
                lblNames.setText(student.getName());
                lblLastNames.setText(student.getLastNames());
                lblId.setText(student.getIdNumber());
                lblProgram.setText(student.getProgram().getName());
                lblCode.setText(student.getCode());
                tblApps.setModel(generateModel(student));
                studentP = student;
            } else {
                Container c = getParent();
                c.setVisible(false);
                c.removeAll();
                c.add(new StudentCreate());
                c.setVisible(true);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnSearchActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCode;
    private javax.swing.JLabel lblId;
    private javax.swing.JLabel lblLastNames;
    private javax.swing.JLabel lblNames;
    private javax.swing.JLabel lblProgram;
    private javax.swing.JTable tblApps;
    private javax.swing.JTextField txtIdNumber;
    // End of variables declaration//GEN-END:variables
}
